<?php
namespace Admin\Model;
use Think\Model;

/**
 * 登录模块
 * @author 
 *        
 */
class LoginModel extends Model
{
    protected $tableName = 'user';
    /**
     * 验证登录表单信息
     * @param string $username
     * @param string $password
     * @param string $verify
     * @return string
     */
    public function check_Login_Form($username='',$password='',$verify=''){
        if (trim($username) == '') {
            return $message ="请输入用户名！";
        }
        if (trim($password) == '') {
            return $message ="请输入密码！";
        }
        
        if (trim($verify) == '') {
            return $message = "请输入验证码";
        }
        if (!$this->check_verify($verify, 'login')) {
            return $message ="验证码错误！";
        }
    }
    
    /**
     * 登录操作
     * @param string $username 用户名
     * @param string $password 密码
     * @return boolean
     */
    public function login($username='',$password=''){
        $model = M("user");
        $user = $model->field('uid,user')->where(array('user' => $username, 'password' => $password))->find();
        
        if ($user) {
            $salt = C("COOKIE_SALT");
            $ip = get_client_ip();
            $ua = $_SERVER['HTTP_USER_AGENT'];
            session_start();
            session(array('name'=>'session_id','expire'=>60));
            session('uid',$user['uid']);
            //加密cookie信息
            $auth = password($user['uid'].$user['user'].$ip.$ua.$salt);
            if ($remember) {
                cookie('auth', $auth, 3600 * 24 * 365);//记住我
            } else {
                cookie('auth', $auth);
            }
            addlog('登录成功。');
            return true;
        } else {
            addlog('登录失败。', $username);
            return false;
        }
    }

    /**
     * 验证码检查
     * @param unknown $code
     * @param string $id
     * @return boolean
     */
    function check_verify($code, $id = '')
    {
        $verify = new \Think\Verify();
        return $verify->check($code, $id);
    }
    
}

?>